<template>
  <div
    class="detail_search"
    :style="[{
      opacity: isShow ? 1 : 0,
      'z-index' : isShow ? 2998 : -1
    }]">
    <div class="search-inner">
      <div 
        class="search_bg"
        @click="handleShowConditionSearch">
        <van-icon name="search" size="0.18rem" />搜索
      </div>
      <!-- 人名，编码 -->
      <div 
        class="content_time_wapper"
        v-show="isShowSearch">
        <gzhb-field
          labelWidth="14%"
          label="姓名：">
          <van-field 
            slot="input" 
            rows="1" 
            autosize 
            v-model="searchName" 
            type="textarea" 
            placeholder="请输入名称" />
        </gzhb-field>
        <gzhb-field
          labelWidth="14%"
          label="工号：">
          <van-field 
            slot="input" 
            rows="1" 
            autosize 
            v-model="searchNo" 
            type="textarea" 
            placeholder="请输入编号" />
        </gzhb-field>
        <div class="content_time_item">
          <div class ="content_time_item_btn" @click="handleSure">确定</div>
          <div class ="content_time_item_btn" @click="handleReset">重置</div>
          <div class ="content_time_item_btn" @click="handleClear">取消</div>
        </div>
      </div>
    </div>
    <div 
      class="search-content" 
      ref="scrollWapper">
        <div class="scroll">
          <van-cell v-for="(item, index) in list" :key="index" :title="item.name" @click="checkHuman(item)"/>
        </div>
    </div>
    <div class="search-btn">
      <div class="search_human">
        <ul>
          <li class="human_name" v-for="(item, index) in humanList" :key="index">{{ item.name }}</li>
        </ul>
      </div>
      <div class="search_number">
        <button @click="handleBeginSearch">
          确定({{ humanNumber }})
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import axios from '@/axios'
import BScroll from 'better-scroll'
export default {
  name: 'DetailSearch',
  props: {
    isShow:{
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,

      // 条件搜索
      isShowSearch: false,

      // 搜索
      searchName: '',
      searchNo: '',
      searchForm:{
        rows: 20,
        page:1,
        sord: 'asc'        
      },
      scrollWapper: '',
      humanList:[]
    }
  },
  computed: {
    humanNumber(){
      return this.humanList.length
    }
  },
  mounted(){
    this.getHumanList()
    this.scrollWapper = new BScroll(this.$refs['scrollWapper'], {
      click: true,
      // 当上拉距离超过50px时触发 pullingUp 事件
      pullUpLoad: {
        threshold: -50 
      }
    })
    this.scrollWapper.on('pullingUp', this.handlePullingUp)
    
  },
  methods: {
    onLoad() {
      this.getHumanList()
        // setTimeout(() => {
        //   for (let i = 0; i < 10; i++) {
        //     this.list.push(this.list.length + 1);
        //   }

        //   // 加载状态结束
        //   this.loading = false;

        //   // 数据全部加载完成
        //   if (this.list.length >= 40) {
        //     this.finished = true;
        //   }
        // }, 1000);
    },

    handlePullingUp(){
      this.getHumanList()
    },

    getHumanList(){
      let newObj ={
        filters: JSON.stringify({
          groupOp: 'AND',
          rules:[
            { field: 'no',op: 'cn',data: this.searchNo },
            { field: 'name',op: 'cn',data: this.searchName }
          ]
        })
      }
       
      // 名称跟工号都没有输入
      if(!this.searchName && !this.searchNo){
        newObj = {}
      }

      axios.post('findLsUser', {
        ...this.searchForm,
        ...newObj
      }).then(res => {
        this.scrollWapper.finishPullUp()
        this.scrollWapper.refresh()
        this.isLoading = false;
        this.searchForm.page += 1
        this.list.push(...res.rows)
      })
    },

    checkHuman(item){
      this.humanList = [item]
      this.$store.commit('SET_SELECTED_HUMAN_LIST', item)
    },

    // 显示条件搜搜
    handleShowConditionSearch(){
      this.isShowSearch = true
    },

    //确定搜索
    handleSure(){
      this.isShowSearch = false
      this.searchForm.page = 1
      this.list = []
      this.getHumanList()
    },

    // 搜索重置
    handleReset(){
      this.searchName = ''
      this.searchNo = ''
    },

    // 取消搜索
    handleClear(){
      this.isShowSearch = false
    },

    // 进行搜索
    handleBeginSearch(){
      this.$store.commit('SET_HUMAN_SEARCH_PAGE', false)
    }
  }
}
</script>

<style lang="less" scoped>
.detail_search{
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  background-color: white;
  z-index: 2998;
  .search-inner{
    position: relative;
    padding: 0.05rem 0.07rem;
    height: 0.4rem;
    color: #9a9a9a;
    background-color: #0089f1; 
    z-index: 999;   
    .search_bg{
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 0.1rem;
      height: 0.3rem;      
      background-color: white;
      span{
        font-size: 0.16rem;
        line-height: 0.17rem;
      }
    }

    // 选择时间
    .content_time_wapper{
      position: absolute;
      top: 0.4rem;
      left: 0;
      padding: 0;
      height: 1.55rem;
      width: 100%;
      background-color: white;
      box-shadow: 0 0 8px 0 #999;
      .content_time_item{
        padding: 0.1rem 0;
        border-top: 1px solid #ddd;
        height: 0.5rem;
        text-align: center;
        color: #9a9a9a;
        font-size: 0.14rem;
        .content_time_item_btn{
          display: inline-block;
          margin-left: 0.1rem;
          border-radius: 0.05rem;
          width: 0.54rem;
          height: 0.3rem;
          line-height: 0.3rem;
          color: white;
          font-size: 0.15rem;
          background-color: #0089f1;
          &:nth-child(1){
            margin-left: 0;
          }
        }
      }
    }
  }

  .search-content{
    flex: 1;
    overflow:hidden;
  }

  .search-btn{
    display: flex;
    // justify-content: center;
    // align-items: center;
    border-top: 1px solid #f0f1f4;
    height: 0.4rem;
    color: white;
    font-size: 0.14rem;
    background-color: white;
    .search_human{
      flex: 1;
      ul{
        padding: 0.05rem 0.12rem;
        height: 100%;
        .human_name{
          display: inline-block;
          padding: 0 0.1rem;
          height: 100%;
          color: black;
          line-height: 0.3rem;
        }
      }
    }
    .search_number{
      display: flex;
      justify-content: center;
      align-items: center;
      width: 0.8rem;
      height: 100%;
      button{
        border-radius: 0.05rem;
        height: 0.32rem;
        width: 0.7rem;
        text-align: center;
        background-color: #267fe0;
      }
    }
  }
}
</style>